package com.kaikeba.controller;

import com.kaikeba.bean.*;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.CourierService;
import com.kaikeba.service.UserService;
import com.kaikeba.util.DateFormatUtil;
import com.kaikeba.util.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 平川
 * @Description:
 * @Date Created in 2021-08-02 22:22
 * @Modified By:
 */
public class UserController {
    @ResponseBody("/user/console3.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        List<Map<String, Integer>> data = UserService.console();
        Message message = new Message();
        if (data.size()==0){
            message.setStatus(-1);
        }else{
            message.setStatus(0);
        }
        message.setData(data);
        String json = JsonUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/user/list2.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        int offset = Integer.parseInt(req.getParameter("offset"));
        int pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
        List<User> list = UserService.findAll(true, offset, pageNumber);
        //时间格式的转换
        List<BootStrapTableUser> list2 = new ArrayList<>();
        for (User u: list){
            String intime = DateFormatUtil.format(u.getIntime());
            String outtime = DateFormatUtil.format(u.getOuttime());
            BootStrapTableUser boot = new BootStrapTableUser(u.getId(),u.getUsername(),u.getPhonenumber(),u.getPassword(),u.getCardid(),intime,outtime);
            list2.add(boot);
        }
        //查询总共的条数
        List<Map<String, Integer>> console = UserService.console();
        int total = console.get(0).get("data_size");
        ResultData<BootStrapTableUser> data = new ResultData<>();
        data.setTotal(total);
        data.setRows(list2);
        String json = JsonUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/user/insert3.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp){
        String username = req.getParameter("username");
        String phonenumber = req.getParameter("phonenumber");
        String cardid = req.getParameter("cardid");
        String password = req.getParameter("password");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setPhonenumber(phonenumber);
        user.setCardid(cardid);
        boolean flag = UserService.insert(user);
        Message message = new Message();
        if (flag){
            message.setStatus(0);
            message.setResult("新增成功！");
        }else{
            message.setStatus(-1);
            message.setResult("新增失败！");
        }
        String json = JsonUtil.toJSON(message);
        return json;
    }
    @ResponseBody("/user/find5.do")
    public String find(HttpServletRequest req, HttpServletResponse resp){
        String number = req.getParameter("number");
        User user = UserService.findByphonenumber(number);
        Message message = new Message();
        if (user==null){
            message.setStatus(-1);
            message.setResult("手机号码不存在");
        }else{
            message.setStatus(0);
            message.setResult("查找成功");
        }
        message.setData(user);
        String json = JsonUtil.toJSON(message);
        return json;
    }
    @ResponseBody("/user/update5.do")
    public String update1(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        String username = req.getParameter("username");
        String phonenumber = req.getParameter("phonenumber");
        String cardid = req.getParameter("cardid");
        String password = req.getParameter("password");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setPhonenumber(phonenumber);
        user.setCardid(cardid);
        boolean flag = UserService.update(id, user);

        Message message = new Message();
        if (flag){
            message.setStatus(0);
            message.setResult("修改成功！");
        }else{
            message.setStatus(-1);
            message.setResult("修改失败！");
        }
        String json = JsonUtil.toJSON(message);
        return json;
    }
    @ResponseBody("/user/delete5.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        boolean flag = UserService.delete(id);
        Message message = new Message();
        if (flag){
            message.setStatus(0);
            message.setResult("删除成功!");
        }else{
            message.setStatus(-1);
            message.setResult("删除失败!");
        }
        String json = JsonUtil.toJSON(message);
        return json;
    }
}
